<template>
  <div class="main">
    <div class="container">
      <h1>欢迎注册疫苗管理系统</h1>
      <el-form ref="form" :model="form" size="normal" :rules="rules">
        <el-form-item prop="account">
          <el-input
            v-model="form.account"
            placeholder="请输入账号"
            @blur="exist"
          ></el-input>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item prop="confirm">
          <el-input
            v-model="form.confirm"
            placeholder="请输入重新密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button style="width: 100%" type="primary" @click="register"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "axios";
axios.defaults.baseURL = "http://localhost:8888";
export default {
  name: "Register",
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.form.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      form: {
        account: "",
        username: "",
        password: "",
        confirm: "",
        role: 2,
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        account: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        confirm: [
          { required: true, validator: validatePass2, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    exist() {
      axios.post(`/user/selectUserByAccount`, this.form).then((respose) => {
        console.log(typeof this.form.account, respose);
        if (respose.data.status === 200) {
          this.$message({
            type: "error",
            message: respose.data.message,
          });
        }
      });
    },
    register() {
      console.log(confirm.same);
      this.$refs["form"].validate((valid) => {
        if (valid) {
          axios.post(`/user/userRegister`, this.form).then((respose) => {
            if (respose.data.status === 200) {
              this.$message({
                type: "success",
                message: "注册成功",
              });
              this.$router.push("/login");
            } else {
              this.$message({
                type: "error",
                message: respose.data.message,
              });
            }
          });
        }
      });
    },
  },
};
</script>

<style scoped>
.main {
  width: 100%;
  height: 100vh;
  background-color: blueviolet;
  margin: 0;
  background-image: url("../images/login1.jpg");
  /* background-size: 100%; */
  /* background-repeat: no-repeat; */
  overflow: hidden;
}
.container {
  width: 400px;
  margin: 100px auto;
}
h1 {
  text-align: center;
  color: white;
  font-size: 30px;
  padding: 30px 0;
}
</style>